<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2017/2/20
 * Time: 13:51
 */

namespace Admin\Controller;


use Common\Controller\BaseController;

class LoginController extends BaseController
{
    public function index(){
        $this->display();
    }
    public function check(){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $code = $_POST['code'];
        if(!$this->check_verify($code)){
            return show(0,'验证码错误');
        }
        if(!trim($username)){
            return show(0,'用户名不能为空');
        }
        if(!trim($password)){
            return show(0,'密码不能为空');
        }

        $res = D('Admin')->getAdminByZhanghao($username);
        if(!$res){
            return show(0,'该用户不存在');
        }
        if($res['password'] != getMd5Password($password)){
            return show(0,'密码错误');
        }
        session('adminUsr',$res);
        return show(1,'登录成功');
    }
    public function loginOut(){
        session_unset();
        session_destroy();
        return show(1,'退出成功');die;
    }

    //生成验证码
    public function verify(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    3,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $verify = new \Think\Verify($config);
        $verify->entry();
    }


// 验证码验证
    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

}